ViewController の描画
概要
ViewController をスクリーン上に描画する方法は、以下の 2 パターンがある。
ViewController を Container View Controller に組み込んで描画する
この場合、基本的なナビゲーションも自動的に提供される
ViewController を直接描画する
モーダル表示をしたいときによく利用される
ViewController を描画すると 関連 が生まれる。描画元が presenting view controller、描画先が presented view controller となる。この関連は presented view controller が消えるまで続く。
ViewController 同士の関連
ViewController の遷移手段
Segue
Interface Builder 内の情報を利用して描画が行われる。
Showing
show(_:sender:) および showDetailViewController(_:sender:) を利用すると、デフォルトでは適切な遷移方法が選択される。例えば、Container View Controller の場合は、モーダル表示の代わりに presented view controller を自身の子として埋め込む、など。これは独自にカスタマイズすることも可能。
呼び出し対象ないし自身の view hierarchy 内の位置も気にしなくてよいし、再利用性が高まため、Presenting より推奨される。
Presenting
present(_:animated:completion:) は、ViewController を常にモーダル表示する。遷移の見え方のカスタマイズはできない。水平方向にコンパクトな要素に適している。
ViewController の遷移時の描画設定
表示する
描画後の外観は Presentation Style、描画時のアニメーションは Transition として設定できる。
1. 遷移先の ViewController を作成する
2. 遷移先の ViewController (presented view controller) の設定を行う
作成した ViewController に modalPresentationStyle で外観を設定する
作成した ViewController に modalTransitionStyle で遷移アニメーションを設定する
(popover の場合: preferredContentSize でサイズを設定する)
(popover の場合: アンカーポイントを設定する)
3. presenting view controller の描画用メソッド(present(_:animated:completion:))を呼び出す
消す
presenting view controller もしくは presented view controller 自身の dismiss(animated:completion:) を呼び出す。presented view controller のものを呼んだ場合でも、UIKit は自動的に presenting view controller にフォワーディングする。presented view controller から presenting view controller に渡したいデータ等がある場合は、delegation パターンを利用するのがよい。
参考
Presenting a View Controller - View Controller Programming Guide for iOS